/**
 * 
 */
package pl.puntin.punclient.view.game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

import pl.puntin.punclient.controller.Controller;

/**
 * @author kakus
 *
 */
public final class GameView extends JPanel
{

	private static final long INTERVAL_BETWEEN_SENDS = 500;
	private static final long DELAY_BEFORE_FIRST_SEND = 1000;
	private final Controller controller;
	private final Timer timer;
	private final DrawPadComponent drawPadComponent;
	
	/**
	 * @param controller
	 */
	public GameView(final Controller controller)
	{
		this.controller = controller;
		setPreferredSize(new Dimension(800, 800));
		drawPadComponent = new DrawPadComponent();
		add(drawPadComponent, BorderLayout.CENTER);
		
		timer = new Timer(true);
	}
	
	/**
	 * Zaczyna wysylac obrazek do serwera.
	 */
	public void startSendingImage()
	{
		timer.schedule(new TimerTask()
		{
			
			@Override
			public void run()
			{
				controller.sendImage(drawPadComponent.getBufferedImage());
			}
		}, DELAY_BEFORE_FIRST_SEND, INTERVAL_BETWEEN_SENDS);
	}

	public void displayImage(final BufferedImage bufferedImage)
	{
		drawPadComponent.setBufferedImage(bufferedImage);
		drawPadComponent.repaint();
	}

}
